# 测试Web应用功能
import requests
import json

def test_web_app():
    base_url = "http://localhost:5001"
    
    print("=== 测试Web应用功能 ===")
    
    # 1. 测试系统状态
    print("\n1. 测试系统状态:")
    try:
        response = requests.get(f"{base_url}/api/status")
        if response.status_code == 200:
            data = response.json()
            print(f"系统状态: {data['message']}")
            print(f"当前模型: {data['model_status']['current_model']}")
            print(f"可用模型: {data['model_status']['available_models']}")
        else:
            print(f"状态检查失败: {response.status_code}")
    except Exception as e:
        print(f"状态检查错误: {e}")
    
    # 2. 测试智源研究院统计
    print("\n2. 测试智源研究院统计:")
    try:
        response = requests.get(f"{base_url}/api/zhiyuan/stats")
        if response.status_code == 200:
            data = response.json()
            stats = data.get('stats', {})
            print(f"论文数量: {stats.get('papers_count', 0)}")
            print(f"数据集数量: {stats.get('datasets_count', 0)}")
            print(f"模型数量: {stats.get('models_count', 0)}")
            print(f"新闻数量: {stats.get('news_count', 0)}")
        else:
            print(f"统计获取失败: {response.status_code}")
    except Exception as e:
        print(f"统计获取错误: {e}")
    
    # 3. 测试聊天功能
    print("\n3. 测试聊天功能:")
    try:
        chat_data = {
            "question": "What is lifetime in Rust?",
            "session_id": "test_session"
        }
        response = requests.post(f"{base_url}/api/chat", json=chat_data)
        if response.status_code == 200:
            data = response.json()
            print(f"问题分类: {data.get('classification', {}).get('label', 'unknown')}")
            print(f"回答长度: {len(data.get('answer', ''))}")
            print(f"包含智源研究院内容: {'智源研究院' in data.get('answer', '')}")
        else:
            print(f"聊天测试失败: {response.status_code}")
    except Exception as e:
        print(f"聊天测试错误: {e}")
    
    print("\n=== 测试完成 ===")

if __name__ == "__main__":
    test_web_app()
